View Javadoc
1 package net.sf.fastxmldb.impl;
2
3 import java.util.Collections;
4 import java.util.Iterator;
5 import java.util.Map;
6 import java.util.Set;
7 import org.apache.commons.collections.SequencedHashMap;
8 import org.xmldb.api.base.Collection;
9 import org.xmldb.api.base.ErrorCodes;
10 import org.xmldb.api.base.Resource;
11 import org.xmldb.api.base.Service;
12 import org.xmldb.api.base.XMLDBException;
13 import org.xmldb.api.modules.XMLResource;
14
15 /***
16 * The Collection implementation. This implementation uses a SequencedhashMap
17 * as the underlying map structure. You are guaranteed that you will get the
18 * resources back in the order in which you added them
19 * @author jbirchfield
20 */
21 public class LocalCollection implements Collection {
22
23 private String name = null;
24
25 private Map resources = null;
26
27 /***
28 * Default constructor.
29 * @param name the name of the collection
30 */
31 public LocalCollection(String name) {
32 this.name = name;
33 resources = new SequencedHashMap();
34 Collections.synchronizedMap(resources);
35 }
36
37 /***
38 *
39 * @see org.xmldb.api.base.Collection#getName()
40 */
41 public String getName() throws XMLDBException {
42 return name;
43 }
44
45 /***
46 * not yet implemented
47 * @see org.xmldb.api.base.Collection#getServices()
48 */
49 public Service[] getServices() throws XMLDBException {
50 throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
51 }
52
53 /***
54 *
55 * @see org.xmldb.api.base.Collection#getService(String, String)
56 */
57 public Service getService(String name, String version)
58 throws XMLDBException {
59 if (name.equals("XPathQueryService")) {
60 // return new LocalXPathQueryService(this);
61 throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
62 }
63
64 if (name.equals("CollectionManagementService")
65 || name.equals("CollectionManager")) {
66 return new LocalCollectionManagementService();
67 }
68 throw new XMLDBException(ErrorCodes.NO_SUCH_SERVICE);
69 }
70
71 /***
72 * not yet implemented
73 * @see org.xmldb.api.base.Collection#getParentCollection()
74 */
75 public Collection getParentCollection() throws XMLDBException {
76 throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
77 }
78
79 /***
80 * not yet implemented
81 * @see org.xmldb.api.base.Collection#getChildCollectionCount()
82 */
83 public int getChildCollectionCount() throws XMLDBException {
84 throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
85 }
86
87 /***
88 * not yet implemented
89 * @see org.xmldb.api.base.Collection#listChildCollections()
90 */
91 public String[] listChildCollections() throws XMLDBException {
92 throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
93 }
94
95 /***
96 * not yet implemented
97 * @see org.xmldb.api.base.Collection#getChildCollection(String)
98 */
99 public Collection getChildCollection(String s) throws XMLDBException {
100 throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
101 }
102
103 /***
104 *
105 * @see org.xmldb.api.base.Collection#getResourceCount()
106 */
107 public int getResourceCount() throws XMLDBException {
108 return resources.size();
109 }
110
111 /***
112 *
113 * @see org.xmldb.api.base.Collection#listResources()
114 */
115 public String[] listResources() throws XMLDBException {
116 Set keys = resources.keySet();
117 String[] ids = new String[keys.size()];
118 int idx = 0;
119 for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
120 String id = (String) iterator.next();
121 ids[idx++] = id;
122 }
123 return ids;
124 }
125
126 /***
127 *
128 * @see org.xmldb.api.base.Collection#createResource(String, String)
129 */
130 public Resource createResource(String docId, String type)
131 throws XMLDBException {
132 XMLResource resource = new LocalXMLResource(docId);
133 resources.put(docId, resource);
134 return resource;
135 }
136
137 /***
138 *
139 * @see org.xmldb.api.base.Collection#removeResource(Resource)
140 */
141 public void removeResource(Resource resource) throws XMLDBException {
142 resources.remove(resource.getId());
143 }
144
145 /***
146 *
147 * @see org.xmldb.api.base.Collection#storeResource(Resource)
148 */
149 public void storeResource(Resource resource) throws XMLDBException {
150 resources.put(resource.getId(), resource);
151 }
152
153 /***
154 *
155 * @see org.xmldb.api.base.Collection#getResource(String)
156 */
157 public Resource getResource(String id) throws XMLDBException {
158 return (Resource) resources.get(id);
159 }
160
161 /***
162 * not yet implemented
163 * @see org.xmldb.api.base.Collection#createId()
164 */
165 public String createId() throws XMLDBException {
166 throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
167 }
168
169 /***
170 * not yet implemented
171 * @see org.xmldb.api.base.Collection#isOpen()
172 */
173 public boolean isOpen() throws XMLDBException {
174 throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
175 }
176
177 /***
178 *
179 * @see org.xmldb.api.base.Collection#close()
180 */
181 public void close() throws XMLDBException {
182 //do nothing...
183 }
184
185 /***
186 * not yet implemented
187 * @see org.xmldb.api.base.Configurable#getProperty(String)
188 */
189 public String getProperty(String s) throws XMLDBException {
190 throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
191 }
192
193 /***
194 * not yet implemented
195 * @see org.xmldb.api.base.Configurable#setProperty(String, String)
196 */
197 public void setProperty(String s, String s1) throws XMLDBException {
198 throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
199 }
200
201 /***
202 * This toString implementation allows you to quickly print out
203 * all of the contained XML documents (as strings).
204 * @see java.lang.Object#toString()
205 */
206 public String toString() {
207 //this is serving us as a shortcut. This toString impl returns the XML
208 //content of the resources. We can use this in clients to avoid looking
209 //up ids and looping over calling the getCOntent method.
210 StringBuffer buf = new StringBuffer();
211 Iterator iter = resources.values().iterator();
212 while (iter.hasNext()) {
213 Resource resource = (Resource) iter.next();
214 try {
215 buf.append(resource.getContent());
216 } catch (XMLDBException e) {
217 e.printStackTrace();
218 }
219 }
220 return buf.toString();
221 }
222
223 /***
224 *
225 * @see java.lang.Object#equals(Object)
226 */
227 public boolean equals(Object o) {
228 if (this == o) {
229 return true;
230 }
231 if (!(o instanceof LocalCollection)) {
232 return false;
233 }
234
235 final LocalCollection localCollection = (LocalCollection) o;
236
237 if (name != null
238 ? !name.equals(localCollection.name)
239 : localCollection.name != null) {
240 return false;
241 }
242 if (resources != null
243 ? !resources.equals(localCollection.resources)
244 : localCollection.resources != null) {
245 return false;
246 }
247
248 return true;
249 }
250
251 }
This page was automatically generated by Maven